package cn.szzsi.common.email;

import com.jfinal.kit.HttpKit;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Created by admin on 2017/6/20.
 */
public class SendEmail {
    public static String send(String email,String text,String title) {
        Properties p = new Properties();
        p.put("mail.smtp.host", "smtp.mxhichina.com");  //smtp服务器地址
        p.put("mail.smtp.port", "25");
        p.put("mail.smtp.auth", "true");
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("9joy@9joydigi.com","!QAZ2wsx");
            }
        };
        //获得一个带有authenticator的session实例
        Session sendMailSession = Session.getDefaultInstance(p,authenticator);
        Message mailMessage = new MimeMessage(sendMailSession);

        //产生6位验证码
        try {
            System.out.println("I'm sending...");
            Address from = new InternetAddress("9joy@9joydigi.com");
            //设置发出方
            //mailMessage.setFrom(from);
            mailMessage.setFrom(new InternetAddress(MimeUtility.encodeWord("微米荟商城")+ " <" +from+ ">"));//设置发出方昵称
            Address to = new InternetAddress(email);//设置接收人员
            mailMessage.setRecipient(Message.RecipientType.TO, to);
            mailMessage.setSubject(title);//设置邮件标题
            mailMessage.setText(text); //设置邮件内容
            // 发送邮件
            Transport.send(mailMessage);
            return "success";
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }

    }

    public static void main(String[] args) {
        /*String email="850971118@qq.com";
        String emailCode=CommonUtil.getSixRandom();
        String text="欢迎您注册微米荟商城。您的验证码为:"+emailCode+",仅供玖悦注册使用,请在30分钟内使用";
        String result=SendEmail.send(email,text);
        System.out.println(result);*/

        Map map=new HashMap();
        map.put("shopId","0");
        map.put("phone","18006217169");
        map.put("content","【微米荟商城】您的企业认证已通过,欢迎使用数字营销平台!");
        map.put("sms_type","ts");
        map.put("app_key","zshop");
        String reqUrl= "https://192.168.0.17:8082"+"/sms/send";//短信服务器接口;
        String result= HttpKit.get(reqUrl,map);
        System.out.println(result);
    }
}
